package com.qnw.CardGroupManagement;
import android.content.*;
import android.text.*;
import android.view.*;
import android.widget.*;
import com.ygo.feihua.*;
import java.util.*;
import android.view.View.*;
import android.graphics.*;
import com.msk86.ygoroid.newutils.*;
import android.support.v4.util.*;

public class TiaoJianAdapter extends BaseAdapter
{
	private List<TiaoJian> data;
	private Context context;
	private Gongju gj;
	private Zujian zujian;
	
	//大于等于或者小于等于,false为小于等于
	Map<Integer,Boolean> maa=new ArrayMap<Integer,Boolean>();
	
	@Override
	public int getCount()
	{
		// TODO: Implement this method
		return data.size();
	}

	@Override
	public Object getItem(int p1)
	{
		// TODO: Implement this method
		return data.get(p1);
	}

	@Override
	public long getItemId(int p1)
	{
		// TODO: Implement this method
		return p1;
	}
	
	
	
	public TiaoJianAdapter(Context context, List<TiaoJian> data)
	{
		this.context=context;
		this.data=data;
		gj=Gongju.getdx(context);
		
		for(int i=0;i<data.size();i++){
			maa.put(i,false);
		}
		
		
	}
	public View getView(final int position, View convertView, ViewGroup parent)
	{
		//final TiaoJian tiaoJian=getItem(position);
		if(convertView==null){
		zujian=new Zujian();
		convertView=LayoutInflater.from(context).inflate(R.layout.item_ksl, null);
		zujian.ksl_name=(TextView) convertView.findViewById(R.id.ksl_name);
		zujian.ksl_jia=(TextView) convertView.findViewById(R.id.ksl_jia);
		zujian.ksl_jian=(TextView) convertView.findViewById(R.id.ksl_jian);
		zujian.ksl_dy=(TextView) convertView.findViewById(R.id.ksl_dy);		
		zujian.ksl_xy=(TextView) convertView.findViewById(R.id.ksl_xy);
		zujian.ksl_number=(TextView) convertView.findViewById(R.id.ksl_number);
			
		convertView.setTag(zujian);
		
		}else{
			zujian=(TiaoJianAdapter.Zujian) convertView.getTag();
		}
		
		zujian.ksl_name.setText(data.get(position).getName());
		zujian.ksl_number.setText(Math.abs( data.get(position).getZs())+"");
		
		if(maa.get(position)){
			zujian.ksl_dy.setBackgroundColor(Color.parseColor( Utils.s( R.color.colorMain)));
			zujian.ksl_dy.setTextColor(Color.parseColor( Utils.s( R.color.white)));

			zujian.ksl_xy.setBackgroundColor(Color.parseColor("#00ffffff"));
			zujian.ksl_xy.setTextColor(Color.parseColor( Utils.s(R.color.colorMain)));
			
			
		}else{
			
			zujian.ksl_xy.setBackgroundColor(Color.parseColor( Utils.s( R.color.colorMain)));
			zujian.ksl_xy.setTextColor(Color.parseColor( Utils.s( R.color.white)));

			zujian.ksl_dy.setBackgroundColor(Color.parseColor("#00ffffff"));
			zujian.ksl_dy.setTextColor(Color.parseColor( Utils.s(R.color.colorMain)));
			
		}
		
		
		zujian.ksl_jia.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					//获取卡片出现数量
			//	String num=tiaoJian.getZs());// zujian.ksl_number.getText().toString();
					Integer in=Math.abs( data.get(position).getZs());
					//如果卡片数量小于3则加一,并更新数列中的元素,否则提示数量不能超过3
					if(in<3){
						in++;
						//如果是大于等于传正数量,否则传负数量
					if(maa.get(position)){
					sxdata(data.get(position),in,position);
					}else{
					sxdata(data.get(position),-in,position);
					}
					
					}else{
						gj.show("卡片数量不能大于3");
					}
					
					// TODO: Implement this method
				}
			});
		zujian.ksl_jian.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					//获取卡片出现数量
				//	String num=zujian.ksl_number.getText().toString();
					Integer in=Math.abs( data.get(position).getZs());
					//如果卡片数量大于0则加一,并更新数列中的元素,否则提示数量不能超过3
					if(in>0){
						in--;
						//如果是大于等于传正数量,否则传负数量
						if(maa.get(position)){
							sxdata(data.get(position),in,position);
						}else{
							sxdata(data.get(position),-in,position);
						}
					}else{
						gj.show("卡片数量不能小于0");
					}
					
					// TODO: Implement this method
				}
			});		
			
		zujian.ksl_dy.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					zujian.ksl_dy.setBackgroundColor(Color.parseColor( Utils.s( R.color.colorMain)));
					zujian.ksl_dy.setTextColor(Color.parseColor( Utils.s( R.color.white)));
				
					zujian.ksl_xy.setBackgroundColor(Color.parseColor("#00ffffff"));
					zujian.ksl_xy.setTextColor(Color.parseColor( Utils.s(R.color.colorMain)));
					maa.put(position,true);
					//获取卡片出现数量
				
					Integer in=Math.abs( data.get(position).getZs());
					sxdata(data.get(position),in,position);
					// TODO: Implement this method
				}
			});		
		zujian.ksl_xy.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					zujian.ksl_xy.setBackgroundColor(Color.parseColor( Utils.s( R.color.colorMain)));
					zujian.ksl_xy.setTextColor(Color.parseColor( Utils.s( R.color.white)));

					zujian.ksl_dy.setBackgroundColor(Color.parseColor("#00ffffff"));
					zujian.ksl_dy.setTextColor(Color.parseColor( Utils.s(R.color.colorMain)));
					
					maa.put(position,false);
					//获取卡片出现数量
					Integer in=Math.abs( data.get(position).getZs());
					sxdata(data.get(position),-in,position);
					
					// TODO: Implement this method
				}
			});		
			
		return convertView;
	}
	class Zujian
	{
		TextView ksl_name,ksl_number,ksl_jia,ksl_jian,ksl_dy,ksl_xy;
	}
	
	private void sxdata(TiaoJian tiaoJian, int num,int position){
		tiaoJian.setZs(num);
		zujian.ksl_number.setText(Math.abs(num)+"");
		data.set(position,tiaoJian);
		notifyDataSetChanged();
		
		
	}
	
	
}
